
import { defineConfig } from 'vite'
import vuePlugin from '@vitejs/plugin-vue'
import eslintPlugin from 'vite-plugin-eslint'
import vueJsx from '@vitejs/plugin-vue-jsx'
import WindiCSS from 'vite-plugin-windicss'
// import pluginLegacy from '@vitejs/plugin-legacy'
import viteCompression from 'vite-plugin-compression'
import viteImagemin from 'vite-plugin-imagemin'
import { VitePWA } from 'vite-plugin-pwa'

import { dirProject } from './lib/global.dir'
import { resolve } from 'path'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vuePlugin({
      template: {
        compilerOptions: {
          isCustomElement: tag => /^((module-|comp-|p-).+?|module)$/.test(tag)
        }
      }
    }),
    eslintPlugin({
      // 配置选项
      cache: false // 禁用 eslint 缓存
    }),
    vueJsx({
      // 配置选项
    }),
    // pluginLegacy({ targets: ['defaults', 'not IE 11'] }),
    viteCompression({
      // 生成压缩包gz
      verbose: true,
      disable: false,
      threshold: 10240,
      algorithm: 'gzip',
      ext: '.gz'
    }),
    viteImagemin({
      gifsicle: {
        optimizationLevel: 7,
        interlaced: false
      },
      optipng: {
        optimizationLevel: 7
      },
      mozjpeg: {
        quality: 20
      },
      pngquant: {
        quality: [0.8, 0.9],
        speed: 4
      },
      svgo: {
        plugins: [
          {
            name: 'removeViewBox'
          },
          {
            name: 'removeEmptyAttrs',
            active: false
          }
        ]
      }
    }),
    WindiCSS({
      scan: {
        dirs: ['.'], // all files in the cwd
        fileExtensions: ['vue'] // also enabled scanning for vue/js/ts
      }
    }),
    VitePWA({
      // mode: 'development',
      base: '/',
      manifest: {
        name: '小锅锅☻Blog',
        short_name: '小锅锅☻Blog',
        theme_color: '#54d9e0',
        background_color: '#ffffff',
        icons: [
          {
            src: '/static/img/icons/android-chrome-48x48.png',
            sizes: '48x48',
            type: 'image/png'
          },
          {
            src: '/static/img/icons/android-chrome-72x72.png',
            sizes: '72x72',
            type: 'image/png'
          },
          {
            src: '/static/img/icons/android-chrome-96x96.png',
            sizes: '96x96',
            type: 'image/png'
          },
          {
            src: '/static/img/icons/msapplication-icon-144x144.png',
            sizes: '144x144',
            type: 'image/png'
          },
          {
            src: '/static/img/icons/android-chrome-168x168.png',
            sizes: '168x168',
            type: 'image/png'
          },
          {
            src: '/static/img/icons/android-chrome-192x192.png',
            sizes: '192x192',
            type: 'image/png'
          },
          {
            src: '/static/img/icons/android-chrome-512x512.png',
            sizes: '512x512',
            type: 'image/png'
          }
        ],
        start_url: '/',
        display: 'standalone',
        lang: 'zh-CN'
      },
      workbox: {
        cacheId: 'vue3-vite-blog',
        runtimeCaching: [
          {
            urlPattern: /api/,
            handler: 'NetworkFirst',
            options: {
              networkTimeoutSeconds: 1,
              cacheName: 'api-cache',
              cacheableResponse: {
                statuses: [0, 200]
              }
            }
          },
          {
            urlPattern: /^https:\/\/cdn\.jsdelivr\.net/,
            handler: 'NetworkFirst',
            options: {
              networkTimeoutSeconds: 1,
              cacheName: 'cdn-cache',
              cacheableResponse: {
                statuses: [0, 200]
              }
            }
          },
          {
            urlPattern: /^https:\/\/fdn\.geekzu\.org/,
            handler: 'NetworkFirst',
            options: {
              networkTimeoutSeconds: 1,
              cacheName: 'avatar-cache',
              cacheableResponse: {
                statuses: [0, 200]
              }
            }
          }
        ]
      }
    })
  ],
  root: resolve(dirProject, 'app'),
  resolve: {
    alias: {
      '~': resolve(dirProject, './'),
      '@': resolve(dirProject, 'app'),
      '@components': resolve(dirProject, 'app/components')
    }
  },
  publicDir: '../public',
  base: './',
  build: {
    outDir: resolve(dirProject, 'dist'),
    emptyOutDir: true,
    chunkSizeWarningLimit: 1024,
    minify: 'terser', // 混淆器,terser构建后文件体积更小
    terserOptions: {
      compress: {
        // 生产环境时移除console.log
        // drop_console: true,
        // drop_debugger: true
      }
    },
    rollupOptions: {
      output: {
        chunkFileNames: 'static/js/[name]-[hash].js',
        entryFileNames: 'static/js/[name]-[hash].js',
        assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
      }
    }
  },
  server: {
    port: 3000,
    host: '0.0.0.0',
    proxy: {
      '^/api/': {
        target: 'http://127.0.0.1:3200',
        changeOrigin: true
      },
      '/prodapi': {
        target: 'http://116.62.145.29:3200',
        secure: false, // 默认情况下（secure: true），不接受在HTTPS上运行的带有无效证书的后端服务器。设置secure: false后，后端服务器的HTTPS有无效证书也可运行
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/prodapi/, '')
      },
      '/betaapi': {
        target: 'http://127.0.0.1:3300',
        secure: false, // 默认情况下（secure: true），不接受在HTTPS上运行的带有无效证书的后端服务器。设置secure: false后，后端服务器的HTTPS有无效证书也可运行
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/betaapi/, '')
      },
      '/wock': {
        target: 'ws://127.0.0.1:10086/wock',
        ws: true
      }
    }
  }
})
